from PySide2.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox

from business.business2 import Calculator


class Page1(QWidget):
    def __init__(self):
        super().__init__()
        # 新增Phi和sin变量
        self.phi = 0.0
        self.sin = 0.0

        # 创建标签和文本框
        labels = [
            "当前井斜", "目标井斜", "垂厚", "地层倾角",
            "斜深", "狗腿度", "剩余视平移"
        ]

        # 为每个文本框分配独立变量
        self.input_field_1 = QLineEdit()
        self.input_field_2 = QLineEdit()
        self.input_field_3 = QLineEdit()
        self.input_field_4 = QLineEdit()
        self.output_field_5 = QLineEdit()
        self.output_field_6 = QLineEdit()
        self.output_field_7 = QLineEdit()

        self.output_field_5.setReadOnly(True)
        self.output_field_6.setReadOnly(True)
        self.output_field_7.setReadOnly(True)

        main_layout = QVBoxLayout()

        for i, label_text in enumerate(labels):
            h_layout = QHBoxLayout()

            label = QLabel(label_text)
            h_layout.addWidget(label)
            if i == 0:
                h_layout.addWidget(self.input_field_1)
            elif i == 1:
                h_layout.addWidget(self.input_field_2)
            elif i == 2:
                h_layout.addWidget(self.input_field_3)
            elif i == 3:
                h_layout.addWidget(self.input_field_4)
            elif i == 4:
                h_layout.addWidget(self.output_field_5)
            elif i == 5:
                h_layout.addWidget(self.output_field_6)
            elif i == 6:
                h_layout.addWidget(self.output_field_7)



            main_layout.addLayout(h_layout)

        # 创建按钮
        button_layout = QHBoxLayout()
        confirm_button = QPushButton("确定")
        reset_button = QPushButton("重置")

        button_layout.addWidget(confirm_button)
        button_layout.addWidget(reset_button)

        main_layout.addLayout(button_layout)

        self.setLayout(main_layout)

        # 绑定按钮事件
        confirm_button.clicked.connect(self.on_confirm)
        reset_button.clicked.connect(self.on_reset)

    def on_confirm(self):
        fields = [self.input_field_1, self.input_field_2, self.input_field_3, self.input_field_4]
        inputs = []
        for field in fields:
            text = field.text().strip()
            if not text:
                QMessageBox.critical(self, "错误", "输入不能为空")
                return
            try:
                value = float(text)
                inputs.append(value)
            except ValueError:
                QMessageBox.critical(self, "错误", "请输入有效数字")
                return

        input1, input2, input3, input4 = inputs[0], inputs[1], inputs[2], inputs[3]

        calc = Calculator()

        # 计算Phi上倾
        self.phi = calc.calculate_angle_up_phi(input1, input2, input4)
        self.sin = calc.calculate_sine(self.phi)
        print("Phi上倾:", self.phi)
        print("sin:", self.sin)

        # 斜深计算（保留整数并四舍五入）
        depth_oblique = round(calc.calculate_depth_oblique(input3, self.sin))
        self.output_field_5.setText(str(depth_oblique))

        # Dogleg度数（保留一位小数并四舍五入）
        dogleg_deg = round(calc.calculate_dogleg_degree(input2, input1, depth_oblique), 1)
        self.output_field_6.setText(str(dogleg_deg))

        # 剩余视平移（保留整数并四舍五入）
        remaining = round(calc.calculate_remaining_gaze_pans_new(input1, input2, depth_oblique))
        self.output_field_7.setText(str(remaining))

    def on_reset(self):
        # 处理重置逻辑
        self.input_field_1.clear()
        self.input_field_2.clear()
        self.input_field_3.clear()
        self.input_field_4.clear()
        self.output_field_5.clear()
        self.output_field_6.clear()
        self.output_field_7.clear()


